﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LdCms.Plugins.Sms.Tencent
{
    using LdCms.Common.Security;
    public static class Utilities
    {
        public static string GetSignature(string appKey, long random, long time)
        {
            try
            {
                StringBuilder builder = new StringBuilder("appkey=")
                    .Append(appKey)
                    .Append("&random=")
                    .Append(random)
                    .Append("&time=")
                    .Append(time);
                return AlgorithmHelper.SHA256(builder.ToString()).ToLower();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public static string GetSignature(string appKey, long random, long time, string mobile)
        {
            try
            {
                StringBuilder builder = new StringBuilder("appkey=")
                    .Append(appKey)
                    .Append("&random=")
                    .Append(random)
                    .Append("&time=")
                    .Append(time)
                    .Append("&mobile=")
                    .Append(mobile);
                return AlgorithmHelper.SHA256(builder.ToString()).ToLower();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public static string GetSignature(string appKey, long random, long time, string[] mobile)
        {
            try
            {
                StringBuilder builder = new StringBuilder("appkey=")
                    .Append(appKey)
                    .Append("&random=")
                    .Append(random)
                    .Append("&time=")
                    .Append(time)
                    .Append("&mobile=");
                if (mobile.Length > 0)
                {
                    builder.Append(mobile[0]);
                    for (int i = 1; i < mobile.Length; i++)
                    {
                        builder.Append(",");
                        builder.Append(mobile[i]);
                    }
                }
                return AlgorithmHelper.SHA256(builder.ToString()).ToLower();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static string MiniClear(this string str)
        {
            try
            {
                string result = str.Replace(" ", "");
                result = result.Replace("\n", "");
                result = result.Replace("\t", "");
                result = result.Replace("\r", "");
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
